<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
请选出你喜欢的开发语言
<select id="s1" multiple>
    <option value="-1" selected>请选择语言</option>
    <option value="0">Java</option>
    <option value="1">JavaScript</option>
</select>
<button onclick="check()">查看选中项的索引</button>
<script>
    let s1 = document.querySelector("#s1");
    console.log(s1.length);
    console.log(s1.options);

    let opt = document.createElement("option");
    opt.text = "PHP";
    // s1.options.add(opt);
    s1.add(opt);

    // 控制Select可以显示的option数量
    s1.size = 10;
    for (let i = 0; i < 10; i++) {
        let opt = document.createElement("option");
        opt.text = i;
        if (i == 4) {
            opt.selected = true;
        }
        s1.add(opt);
    }

    function check() {
        // 只会有第一个选中项的索引，不支持多选
        console.log(s1.selectedIndex);
        console.log(s1.selectedOptions);
        console.log(s1.value);
    }
</script>
</body>
</html>